//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DoublingConvergenceSteps.h"
using namespace Cephei::QL::Math::Statistics;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (boost::shared_ptr<QuantLib::DoublingConvergenceSteps>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDoublingConvergenceSteps = NULL;
#endif
	_ppDoublingConvergenceSteps = &childNative;
    
}
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (QuantLib::DoublingConvergenceSteps& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDoublingConvergenceSteps = NULL;
#endif
	_ppDoublingConvergenceSteps = new boost::shared_ptr<QuantLib::DoublingConvergenceSteps> (&childNative);
    
    _DoublingConvergenceStepsOwner = owner;
    
}

Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (CDoublingConvergenceSteps^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDoublingConvergenceSteps = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDoublingConvergenceSteps = new boost::shared_ptr<QuantLib::DoublingConvergenceSteps> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDoublingConvergenceSteps = NULL;
#endif
	if (!t->IsSubclassOf(CDoublingConvergenceSteps::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (QuantLib::Handle<QuantLib::DoublingConvergenceSteps>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDoublingConvergenceSteps = &childNative;
	_ppDoublingConvergenceSteps = &static_cast<boost::shared_ptr<QuantLib::DoublingConvergenceSteps>>(childNative.currentLink());
    
    _DoublingConvergenceStepsOwner = owner;
}
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (QuantLib::Handle<QuantLib::DoublingConvergenceSteps> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDoublingConvergenceSteps = &childNative;
	_ppDoublingConvergenceSteps = &static_cast<boost::shared_ptr<QuantLib::DoublingConvergenceSteps>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::CDoublingConvergenceSteps (QuantLib::DoublingConvergenceSteps childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDoublingConvergenceSteps = NULL;
#endif
	_ppDoublingConvergenceSteps = new boost::shared_ptr<QuantLib::DoublingConvergenceSteps> (new QuantLib::DoublingConvergenceSteps (childNative));
    
}
#endif

Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::~CDoublingConvergenceSteps ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDoublingConvergenceSteps != NULL)
    {
	    delete _ppDoublingConvergenceSteps;
        _ppDoublingConvergenceSteps = NULL;
    }
}
Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::!CDoublingConvergenceSteps ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDoublingConvergenceSteps != NULL)
    {
	    delete _ppDoublingConvergenceSteps;
    }
}
QuantLib::DoublingConvergenceSteps& Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::GetReference ()
{
    if (_ppDoublingConvergenceSteps == NULL) throw gcnew NativeNullException ();
	return **_ppDoublingConvergenceSteps;
}
boost::shared_ptr<QuantLib::DoublingConvergenceSteps>& Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::GetShared ()
{
    if (_ppDoublingConvergenceSteps == NULL) throw gcnew NativeNullException ();
	return *_ppDoublingConvergenceSteps;
}
QuantLib::DoublingConvergenceSteps* Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::GetPointer ()
{
    if (_ppDoublingConvergenceSteps == NULL) throw gcnew NativeNullException ();
	return &**_ppDoublingConvergenceSteps;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DoublingConvergenceSteps>& Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::GetHandle ()
{
	if (_phDoublingConvergenceSteps == NULL)
	{
		_phDoublingConvergenceSteps = new Handle<QuantLib::DoublingConvergenceSteps> (*_ppDoublingConvergenceSteps);
	}
	return *_phDoublingConvergenceSteps;
}
#endif
bool Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::HasNative () 
{
	return (_ppDoublingConvergenceSteps != NULL);
}

UInt64 Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::InitialSamples::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppDoublingConvergenceSteps)->initialSamples ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Statistics::CDoublingConvergenceSteps::NextSamples (UInt64 current)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _current = (QuantLib::Size)ValueHelper::Convert (current);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppDoublingConvergenceSteps)->nextSamples ( _current );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

